package com.hoodbrains.insideloc.maps;

import java.util.ArrayList;
import java.util.List;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.Log;

import com.hoodbrains.insideloc.R;

import com.hoodbrains.insideloc.shopping.ShoppingItem;
import com.hoodbrains.insideloc.shopping.ShoppingList;

public class Liste extends DrawableObject {
	private Paint paint;
	private List<String> rayons;
	private Magasin mMagasin;
	private int currX, currY;
	private int prodX, prodY;
	public Bitmap client,product;
	public Liste( Magasin mMagasin) {
		this.rayons = new ArrayList<String>();
		this.mMagasin = mMagasin;
		this.currX = 0;
		this.currY = 0;
		List<ShoppingItem> L = ShoppingList.getInstance().getItemList();
		
		for (ShoppingItem sItem : L) {
			if (!sItem.isGrabbed())
				this.rayons.add(sItem.getRayon());
		}

		this.paint = new Paint();

	}			

	private void setProductPosition() {
		if (this.rayons.size() == 0) {
			this.prodX = this.mMagasin.getRayons().get("Caisse").x;
			this.prodY = this.mMagasin.getRayons().get("Caisse").y;
			return;
		}

		//test empeche le Forceclose quand on choisit un produit
		//qui n'est pas dans le magasin
		if(this.mMagasin.getRayons().containsKey(this.rayons.get(0))){
			this.prodX = this.mMagasin.getRayons().get(this.rayons.get(0)).x;
			this.prodY = this.mMagasin.getRayons().get(this.rayons.get(0)).y;
		}
	}

	public void update() {
		this.currX = this.prodX;
		this.currY = this.prodY;
		if (this.rayons.size() == 0){
			
			return;
		}
		this.rayons.remove(0);
		this.setProductPosition();
		
	}

	private void drawProduct(Canvas canvas) {
		//this.paint.setColor(MAGENTA);
		Rect rect = new Rect(this.prodX - 15 , this.prodY - 15 , this.prodX + 15 , this.prodY + 15);
		canvas.drawBitmap(this.product, null, rect, this.paint);
	}

	private void drawClient(Canvas canvas) {
		Rect rect = new Rect(this.currX - 15 , this.currY - 25 , this.currX + 15 , this.currY + 20);
		canvas.drawBitmap(this.client, null, rect, this.paint);
	}

	public void draw(Canvas canvas) {
		paint = new Paint();
		this.setProductPosition();
		this.drawClient(canvas);
		this.drawProduct(canvas);
	}

}
